package com.easemob.chat.core;

import android.content.Context;
import android.text.TextUtils;
import android.util.Xml;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatConfig;
import com.easemob.cloud.HttpClientManager;
import com.easemob.util.CryptoUtils;
import com.easemob.util.EMLog;
import com.easemob.util.NetUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class f
{
  private static final String a = f.class.getSimpleName();
  private static final String b = "file_version";
  private static final String c = "app_key";
  private static final String d = "sdk_version";
  private static final int e = 5;
  private String f = "easemob";
  private String g = "server.xml";
  private int h = 5;
  private a i = null;
  private static final String j = "http://www.easemob.com/easemob/server.xml";
  private static final String k = "com.easemob.config.xml";
  private static final String l = "com.easemob.config.ky.xml";
  private static long m = 259200000L;
  private static f n = new f();
  private i o = null;
  private boolean p = false;
  private Object q = new Object();

  public static f a()
  {
    return n;
  }

  public void a(Context paramContext)
  {
  }

  public c b()
  {
    return this.o.b();
  }

  public c c()
  {
    return this.o.c();
  }

  public c d()
  {
    return this.o.d();
  }

  public c e()
  {
    return this.o.h();
  }

  public c f()
  {
    return this.o.i();
  }

  public c g()
  {
    return this.o.j();
  }

  public synchronized a h()
  {
    if (this.i != null)
      return this.i;
    Object localObject;
    if (k.a().x() == -1L)
    {
      localObject = j();
      if (localObject != null)
        this.i = ((a)localObject);
    }
    else
    {
      localObject = new CryptoUtils();
      ((CryptoUtils)localObject).initAES();
      try
      {
        String str = ((CryptoUtils)localObject).decryptBase64String(k.a().w());
        this.i = a(str);
        m();
      }
      catch (Exception localException)
      {
        EMLog.e(a, "parse dns xml from our store is failed with error : " + localException.getMessage());
      }
      if (System.currentTimeMillis() - k.a().u() > 0L)
        this.i = j();
    }
    return this.i;
  }

  public synchronized void i()
  {
    try
    {
      k localk = k.a();
      localk.a(-1L);
      localk.b(-1L);
      localk.e("");
      localk.d("");
      this.o.k();
      this.i = null;
    }
    catch (Exception localException)
    {
    }
  }

  public a j()
  {
    if (this.p)
    {
      synchronized (this.q)
      {
        try
        {
          this.q.wait();
        }
        catch (InterruptedException localInterruptedException)
        {
          localInterruptedException.printStackTrace();
        }
      }
      if (this.i != null)
        return this.i;
    }
    synchronized (this.q)
    {
      this.p = true;
      a locala = null;
      for (int i1 = 0; i1 < this.h; i1++)
      {
        EMLog.d(a, "try to retrieve dns config! with retries number : " + i1);
        locala = k();
        if (locala != null)
          this.i = locala;
        else
          if (!NetUtils.hasDataConnection(EMChat.getInstance().getAppContext()))
            break;
      }
      if (locala == null)
      {
        String str = null;
        if (k.a().g())
          str = k.a().c("com.easemob.config.ky.xml");
        else
          str = k.a().c("com.easemob.config.xml");
        if ((str != null) && (!str.equals("")))
          locala = a(str);
        if (locala != null)
        {
          this.i = locala;
          m();
        }
      }
      this.p = false;
      this.q.notifyAll();
      return locala;
    }
  }

  private a k()
  {
    a locala = null;
    try
    {
      HashMap localHashMap = new HashMap();
      str1 = l();
      EMLog.d(a, "config server url : " + str1);
      HttpResponse localHttpResponse = HttpClientManager.httpExecute(str1, localHashMap, null, HttpClientManager.Method_GET);
      int i1 = localHttpResponse.getStatusLine().getStatusCode();
      if (i1 == 200)
      {
        HttpEntity localHttpEntity = localHttpResponse.getEntity();
        String str2 = EntityUtils.toString(localHttpEntity);
        EMLog.d(a, "returned config content : " + str2);
        locala = a(str2);
        if (locala != null)
          a(locala, str2);
      }
    }
    catch (Exception localException)
    {
      String str1 = "error to retrieve dns config";
      if ((localException != null) && (localException.getMessage() != null))
        str1 = localException.getMessage();
      EMLog.e(a, "retrieveDNSConfigWithCountDown error:" + str1);
      if (str1.contains("refused"))
        this.o.h();
    }
    return locala;
  }

  private String l()
    throws UnsupportedEncodingException
  {
    String str = "http://www.easemob.com/easemob/server.xml";
    if (this.i != null)
    {
      c localc = this.o.d();
      StringBuilder localStringBuilder = new StringBuilder();
      if ((localc.c == null) || (!localc.c.contains("http")))
        localStringBuilder.append("http");
      else
        localStringBuilder.append(localc.c);
      localStringBuilder.append("://");
      localStringBuilder.append(localc.a + "/" + this.f + "/" + this.g);
      str = localStringBuilder.toString();
    }
    str = str + "?" + "sdk_version" + "=" + URLEncoder.encode(k.a().b(), "UTF-8") + "&" + "app_key" + "=" + URLEncoder.encode(EMChatConfig.getInstance().APPKEY, "UTF-8") + "&" + "file_version" + "=" + URLEncoder.encode(k.a().v(), "UTF-8");
    return str;
  }

  private void a(a parama, String paramString)
    throws Exception
  {
    k localk = k.a();
    String str = localk.v();
    if (parama != null)
    {
      if ((TextUtils.isEmpty(str)) || (!str.equals(parama.b)))
      {
        CryptoUtils localCryptoUtils = new CryptoUtils();
        localCryptoUtils.initAES();
        localk.e(localCryptoUtils.encryptBase64String(paramString));
        localk.d(parama.b);
      }
      localk.a(System.currentTimeMillis());
      if (System.currentTimeMillis() >= parama.c)
        localk.b(System.currentTimeMillis() + m);
      else
        localk.b(parama.c);
    }
  }

  public a a(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")))
      return null;
    return a(new ByteArrayInputStream(paramString.getBytes()));
  }

  synchronized a a(InputStream paramInputStream)
  {
    if (paramInputStream == null)
      return null;
    a locala = null;
    try
    {
      XmlPullParser localXmlPullParser = Xml.newPullParser();
      localXmlPullParser.setInput(paramInputStream, "UTF-8");
      for (int i1 = localXmlPullParser.getEventType(); i1 != 1; i1 = localXmlPullParser.next())
        if (i1 == 2)
        {
          String str = localXmlPullParser.getName();
          if ("ebs".equals(str))
          {
            locala = new a();
          }
          else if ("deploy_name".equals(str))
          {
            i1 = localXmlPullParser.next();
            if (locala != null)
              locala.a = localXmlPullParser.getText();
          }
          else if ("file_version".equals(str))
          {
            i1 = localXmlPullParser.next();
            if (locala != null)
              locala.b = localXmlPullParser.getText();
          }
          else if ("valid_before".equals(str))
          {
            i1 = localXmlPullParser.next();
            if (locala != null)
              try
              {
                locala.c = Integer.parseInt(localXmlPullParser.getText());
                if (locala.c <= 0L)
                  locala.c = (System.currentTimeMillis() + m);
                else
                  locala.c *= 1000L;
              }
              catch (Exception localException)
              {
                EMLog.d(a, localException.getMessage());
                locala.c = (System.currentTimeMillis() + m);
              }
          }
          else if ("im".equals(str))
          {
            i1 = localXmlPullParser.next();
            if (locala != null)
            {
              locala.e = new ArrayList();
              a(localXmlPullParser, locala.e);
            }
          }
          else if ("rest".equals(str))
          {
            i1 = localXmlPullParser.next();
            if (locala != null)
            {
              locala.f = new ArrayList();
              a(localXmlPullParser, locala.f);
            }
          }
          else if ("resolver".equals(str))
          {
            i1 = localXmlPullParser.next();
            if (locala != null)
            {
              locala.d = new ArrayList();
              a(localXmlPullParser, locala.d);
            }
          }
        }
    }
    catch (XmlPullParserException localXmlPullParserException)
    {
      localXmlPullParserException.printStackTrace();
      locala = null;
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
      locala = null;
    }
    return locala;
  }

  private void a(XmlPullParser paramXmlPullParser, List<b> paramList)
    throws XmlPullParserException, IOException
  {
    if (paramList == null)
      return;
    int i1 = paramXmlPullParser.getEventType();
    b localb = null;
    while (true)
    {
      String str;
      if (i1 == 2)
      {
        str = paramXmlPullParser.getName();
        if (str.equals("host"))
        {
          localb = new b();
        }
        else if (str.equals("domain"))
        {
          paramXmlPullParser.next();
          if (localb != null)
            localb.a = paramXmlPullParser.getText();
        }
        else if (str.equals("ip"))
        {
          paramXmlPullParser.next();
          if (localb != null)
            localb.b = paramXmlPullParser.getText();
        }
        else if (str.equals("port"))
        {
          paramXmlPullParser.next();
          if (localb != null)
            try
            {
              localb.c = Integer.parseInt(paramXmlPullParser.getText());
            }
            catch (Exception localException)
            {
              EMLog.d(a, localException.getMessage());
              localb.c = -1;
            }
        }
        else if (str.equals("protocol"))
        {
          paramXmlPullParser.next();
          if (localb != null)
            localb.d = paramXmlPullParser.getText();
        }
      }
      else if (i1 == 3)
      {
        str = paramXmlPullParser.getName();
        if (str.equals("host"))
        {
          if (localb != null)
            paramList.add(localb);
        }
        else if (str.equals("hosts"))
          break;
      }
      else if (i1 == 1)
      {
        EMLog.w(a, "we reached end of document, but not end of hosts!");
        break;
      }
      i1 = paramXmlPullParser.next();
    }
  }

  private void m()
  {
    if (this.i != null)
    {
      if (this.i.e != null)
        a(this.i.e);
      if (this.i.f != null)
        a(this.i.f);
      if (this.i.d != null)
        a(this.i.d);
    }
  }

  private void a(List<b> paramList)
  {
    if ((paramList != null) && (paramList.size() > 1))
    {
      ArrayList localArrayList = new ArrayList();
      localArrayList.addAll(paramList);
      int i1 = paramList.size();
      paramList.clear();
      for (int i2 = 0; i2 < i1; i2++)
      {
        int i3 = 0;
        if (i2 < i1 - 1)
          i3 = new Random().nextInt(i1 - 1 - i2);
        b localb = (b)localArrayList.remove(i3);
        paramList.add(localb);
      }
    }
  }

  public static class a
  {
    public String a = "";
    public String b = "";
    public long c = -1L;
    public List<f.b> d;
    public List<f.b> e;
    public List<f.b> f;

    public String a()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      if (this.a != null)
        localStringBuilder.append("name : " + this.a + "\n");
      if (this.b != null)
        localStringBuilder.append("version : " + this.b + "\n");
      localStringBuilder.append("valid_before : " + this.c + "\n");
      if (this.d != null)
        localStringBuilder.append(this.d.toString());
      if (this.e != null)
        localStringBuilder.append(this.e.toString());
      if (this.f != null)
        localStringBuilder.append(this.f.toString());
      return localStringBuilder.toString();
    }

    public boolean a(Object paramObject)
    {
      if (paramObject == this)
        return true;
      if (paramObject == null)
        return false;
      if ((paramObject instanceof a))
      {
        a locala = (a)paramObject;
        if ((locala.a.equals(this.a)) && (locala.b.equals(this.b)) && (locala.c == this.c))
        {
          if (((this.e == null) && (locala.e != null)) || ((this.e != null) && (locala.e == null)))
            return false;
          if (((this.f == null) && (locala.f != null)) || ((this.f != null) && (locala.f == null)))
            return false;
          if ((this.e != null) && (!this.e.equals(locala.e)))
            return false;
          return (this.f == null) || (this.f.equals(locala.f));
        }
        return false;
      }
      return false;
    }
  }

  public static class b
  {
    public String a = "";
    public String b = "";
    public int c = 0;
    public String d = "";

    public String a()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("domain : " + this.a + "\n");
      localStringBuilder.append("ip : " + this.b + "\n");
      localStringBuilder.append("port : " + this.c + "\n");
      localStringBuilder.append("protocol : " + this.d + "\n");
      return localStringBuilder.toString();
    }

    public boolean a(Object paramObject)
    {
      if (paramObject == this)
        return true;
      if (paramObject == null)
        return false;
      if ((paramObject instanceof b))
      {
        b localb = (b)paramObject;
        return (localb.a.equals(this.a)) && (localb.b.equals(this.b)) && (localb.c == this.c) && (localb.d.equals(this.d));
      }
      return false;
    }
  }

  public static class c
  {
    public String a = null;
    public int b = -1;
    public String c = "";
    public f.b d = null;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.core.f
 * JD-Core Version:    0.6.1
 */